package com.gouda.core.dao;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.gouda.core.entity.SystemUserEntity;
import com.themis.entity.SystemUserRoleEntity;
import com.themis.enums.EnumIsEnable;
import org.amethyst.atlas.basic.manufactory.DBEntityIFactory;
import org.amethyst.atlas.basic.manufactory.specifics.DBEntityFactory;
import org.amethyst.atlas.db.MagicDao;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * system user dao
 * Created by KGL on 2015/8/13.
 */
@Repository
public class SystemUserDao extends MagicDao {

    protected static final DBEntityIFactory ENTITY_FACTORY = DBEntityFactory.getFactory();

    private static final String QUERY_SQL = "select t.user_id, t.user_code, t.user_name, t.user_status, t.user_type, t.creator_name, t.create_time, t.modifier, t.modify_time from system_user t";

    private static final String QUERY_COUNT = "select 1 from system_user t";

    public int seekUserCountByPrivilege(final String userType, final String userCode, final String userName, final String userStatus){
        if (Strings.isNullOrEmpty(userType)) return 0;
        final Map<String, Object> params = Maps.newHashMap();
        StringBuilder whereSql = new StringBuilder(" where t.user_type >= :userType ");
        params.put("userType", userType);
        if (!Strings.isNullOrEmpty(userCode)){
            whereSql.append(" and t.user_code = :userCode");
            params.put("userCode", userCode);
        }
        if (!Strings.isNullOrEmpty(userName)){
            whereSql.append(" and t.user_name = :userName");
            params.put("userName", userName);
        }
        if (!Strings.isNullOrEmpty(userStatus)){
            whereSql.append(" and t.user_status = :userStatus");
            params.put("userStatus", userStatus);
        }

        List<SystemUserEntity> userEntities = super.list(SystemUserEntity.class, QUERY_COUNT + whereSql.toString(), params);
        return userEntities.size();
    }

    public List<SystemUserEntity> seekUsersByPrivilege(final String userType, final String userCode, final String userName, final String userStatus, final int start, final int size){
        if (Strings.isNullOrEmpty(userType)) return null;
        final Map<String, Object> params = Maps.newHashMap();
        StringBuilder whereSql = new StringBuilder(" where t.user_type >= :userType ");
        params.put("userType", userType);
        if (!Strings.isNullOrEmpty(userCode)){
            whereSql.append(" and t.user_code = :userCode");
            params.put("userCode", userCode);
        }
        if (!Strings.isNullOrEmpty(userName)){
            whereSql.append(" and t.user_name = :userName");
            params.put("userName", userName);
        }
        if (!Strings.isNullOrEmpty(userStatus)){
            whereSql.append(" and t.user_status = :userStatus");
            params.put("userStatus", userStatus);
        }
        whereSql.append(" order by t.create_time desc limit :start,:size");
        params.put("start", start);
        params.put("size", size);
        return super.list(SystemUserEntity.class, QUERY_SQL + whereSql.toString(), params);
    }

    /**
     * seek system user obj by user code
     * @param userCode
     * @return
     */
    public List<SystemUserEntity> seekByUserCode(final String userCode){
        final String whereSql = " where t.user_code = :userCode";
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return super.list(SystemUserEntity.class, QUERY_SQL + whereSql, params);
    }

    public void batchAuth(final String userId, final String[] roleArray){
        if (delAuthRoles(userId)){
            for (String roleId : roleArray) {
                SystemUserRoleEntity userRoleEntity = ENTITY_FACTORY.generateDBEntity(SystemUserRoleEntity.class);
                userRoleEntity.setUserId(Long.valueOf(userId));
                userRoleEntity.setRoleId(Long.valueOf(roleId));
                userRoleEntity.setEnable(EnumIsEnable.YES.getCode());
                insert(userRoleEntity);
            }
        }
    }

    private boolean delAuthRoles(final String userId){
        if (Strings.isNullOrEmpty(userId)) return false;
        final String delSql = "delete from system_user_role where user_id = :userId";
        Map<String, Object> params = Maps.newHashMap();
        params.put("userId", userId);
        return executeNativeSql(SystemUserRoleEntity.class, delSql, params);
    }

    public SystemUserEntity login(final String userCode, final String pwd){
        if (Strings.isNullOrEmpty(userCode) || Strings.isNullOrEmpty(pwd)){
            return null;
        }
        final String whereSql = " where t.user_code = :userCode and t.user_password = :pwd and t.user_status = 1";
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("pwd", pwd);
        List<SystemUserEntity> entites = super.list(SystemUserEntity.class, QUERY_SQL + whereSql, params);
        if (null == entites || entites.isEmpty()) return null;
        return entites.get(0);
    }
}
